$(function(){
	initBlockUI();
	updateTable(0, "#pager");
	initPagination();
});

// 分页
function initPagination()
{
	$("#pager").pagination(
		$("#wholepage").val(),
		{
			items_per_page: 10,
			prev_text: "前一页",
			next_text: "后一页",
			callback: updateTable
		}
	);
}

// 更新数据表格
function updateTable(page, pager_block)
{
	var url = "news_received.php";
	var pars = {
		"page_status": 1,
		"page": (page + 1)
	};
	var str = "";
	$.post(url, pars, function(xmlobj){
		$("#tbody2").html(" ");
		$(xmlobj).find("item").each(function(i){
			var id = $(this).find("id").text();
			var title = $(this).find("title").text();
			var author = $(this).find("author").text();
			var addtime = $(this).find("addtime").text().split(" ")[0];
			$("<tr></tr>")
				.append($("<th></th>").html(i + 1))
				.append($("<td></td>")
					.append($("<a href=\"#\"></a>").html(title).click(function(){
						openModal(id);
						return false;
					})))
				.append($("<td></td>").html(author))
				.append($("<td></td>").html(addtime))
			.appendTo("#tbody2");
		});
		tablePretty("#tbody2");
	});
	return false;
}

// 使用 jquery.simplemodal 打开新闻
function openModal(news_id)
{
	var url = "news_received.php";
	var pars = {"page_status" : 2, "news_id": news_id};
	$.post(url, pars, function(xmlobj){
		var title = $(xmlobj).find("title").text().unescapeString();
		var content = $(xmlobj).find("content").text().unescapeString();
		var preview_block = $("<div></div>")
			.append($("<div></div>").addClass("modal_header").html(title))
			.append($("<div></div>").addClass("modal_content").html(content));
		$.modal(preview_block);
	});
}

